"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : tinyblob数据类型
Case Name   : gs_dump支持tinyblob类型的数据
Create At   : 2022.08.11
@zou_jialiang0501129488
Description :
    1、创建b库
    2、b库下查询dolphin插件
    3、gs_dump支持tinyblob类型的数据
    4、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class DatatypeTest01(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.constant = Constant()
        self.dump_path = os.path.join(macro.DB_INSTANCE_PATH,
                                      'Tinyblob_Case0044.sql')
        self.db_name = 'db_tinyblob_0044'

    def test_tinyblob(self):
        text = '-----step1:创建兼容b库的数据库;expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} " \
                  f"dbcompatibility ='B' encoding ='UTF8';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)

        text = '-----step2:b库下查询dolphin插件;expect:自动加载成功,无需手动创建-----'
        self.log.info(text)
        sql_cmd = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn('extname', sql_res, '查询失败')
        self.assertIn('dolphin', sql_res, '执行失败' + text)

        text = '-----step3:gs_dump支持tinyblob类型的数据;expect:成功-----'
        self.log.info(text)
        cmd = f"rm -rf {self.dump_path};touch {self.dump_path};" \
              f"ls {macro.DB_INSTANCE_PATH} | grep 'Tinyblob_Case0044.sql';"
        self.log.info(cmd)
        res = self.common.get_sh_result(self.user_node, cmd)
        self.assertIn('Tinyblob_Case0044.sql', res, '文件创建失败')

        self.log.info('-----建表插数据-----')
        sql = f"drop table if exists t_tinyblob_044;" \
              f"create table t_tinyblob_044(field_name tinyblob);" \
              f"insert into t_tinyblob_044 values ('aaa'::blob);" \
              f"insert into t_tinyblob_044 values ('bbb'::blob);" \
              f"insert into t_tinyblob_044 values ('ccc'::blob);" \
              f"insert into t_tinyblob_044 values ('ddd'::blob);"
        self.log.info(sql)
        res = self.sh_primary.execut_db_sql(sql, dbname=self.db_name)
        self.assertIn('CREATE TABLE', res, '建表失败')
        self.assertEqual(res.count('INSERT 0 1'), 4, '插入数据失败')

        sql_select = f"select * from t_tinyblob_044;"
        self.log.info(sql_select)
        check_msg = self.sh_primary.execut_db_sql(sql_select,
                                                  dbname=self.db_name)
        self.log.info(check_msg)

        self.log.info('-----使用gs_dump备份数据-----')
        dump_res = self.sh_primary.exec_gs_dump(self.dump_path,
                                                '-t t_tinyblob_044',
                                                self.db_name, get_detail=True)
        self.assertIn(f"dump database {self.db_name} successfully",
                      dump_res, '建表失败')

        self.log.info('-----删除已备份表-----')
        sql = 'drop table if exists t_tinyblob_044;'
        self.log.info(sql)
        res = self.sh_primary.execut_db_sql(sql, dbname=self.db_name)
        self.log.info(res)
        self.assertIn('DROP TABLE', res, '表删除失败')

        self.log.info('-----使用gsql导入数据-----')
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"gsql -d {self.db_name} -p {self.user_node.db_port} " \
              f"-U {self.user_node.ssh_user} " \
              f"-W {self.user_node.ssh_password} " \
              f"-f {self.dump_path}"
        self.log.info(cmd)
        res = self.common.get_sh_result(self.user_node, cmd)
        self.log.info(res)
        self.assertIn('ALTER TABLE', res, 'gsql恢复失败')

        res = self.sh_primary.execut_db_sql(sql_select, dbname=self.db_name)
        self.log.info(res)
        self.assertEqual(check_msg, res, '文件数据至表失败')

    def tearDown(self):
        self.log.info('-----step4:清理环境; expect:成功-----')
        check_cmd = f'rm -rf {self.dump_path};' \
                    f'if [ -d {self.dump_path} ]; ' \
                    f'then echo "exists"; else echo "not exists"; fi'
        self.log.info(check_cmd)
        res = self.common.get_sh_result(self.user_node, check_cmd)
        sql_cmd = f"drop database if exists {self.db_name};"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        self.assertEqual('not exists', res, '文件删除失败')
        self.assertIn('DROP DATABASE', sql_res, '数据库创建失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
